﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SpeechLib;
using System.Threading;
using SpellingBeeAssistant.com.aonaware.services;
namespace SpellingBeeAssistant
{
    public partial class SBUserInterface : Form
    { 
        IDictionary<int, string> id = new Dictionary<int, string>();
        Random random = new Random();
        string _currWord = string.Empty;
        IDictionary<int, bool> processed = new Dictionary<int, bool>();
        SpVoice _objSpeech = new SpVoice();
        int _incorrectWords = 0;
        private string _currWordFile;

        public SBUserInterface()
        {
            InitializeComponent();
        }

        private void ReInitWords()
        {
            id = new Dictionary<int, string>();
            processed = new Dictionary<int, bool>();
            System.IO.StreamReader file =
                new System.IO.StreamReader(_currWordFile);
            string line;
            int counter = 0;

            while ((line = file.ReadLine()) != null)
            {
                id[++counter] = line;
            }
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            MoveToNextWord();
            checkWord.Focus();

        }

        private void Definition_Click(object sender, System.EventArgs e)
        {
            try
            {
                DictService dict = new DictService();
                WordDefinition wd = dict.Define(_currWord);
                int longestDefIndex = 0, longestDefLength = 0;
                string[] defs = wd.Definitions[0].WordDefinition.Replace("\n", "").Replace("  ", " ").Split("][".ToCharArray());
                for (int i = 0; i < defs.Length; i++)
                {
                    if (defs[i].Length > longestDefLength)
                    {
                        longestDefLength = defs[i].Length;
                        longestDefIndex = i;
                    }
                }
                SpeakWord(defs[longestDefIndex]);
            }
            catch (Exception)
            {
            }
        }


        private void MoveToNextWord()
        {
            if (string.IsNullOrWhiteSpace(_currWordFile))
            {
                SpeakWord(@"Please select a word list  by clicking on the  ""Choose Word List button"" ");
            }
            else
            {
                int? nextID = GetNextID();
                if (nextID != null)
                {
                    _currWord = id[(int)nextID];
                    displayWord.Text = string.Empty;
                    SpeakWord(_currWord);

                }
                else
                {
                    SpeakWord("Done cycling through all the words");

                }
                checkWord.Focus();
            }
        }

        private void SpeakWord(string word)
        {
            _objSpeech.Speak(word);
            //_objSpeech.WaitUntilDone(Timeout.Infinite);
        }

        private int? GetNextID()
        {
            int? returnCd = null;
            for (int i = 0; i < id.Count * 2 ; i++)
            {
                if (processed.Count >= id.Count)
                    break;
                returnCd = random.Next(id.Count+ 1);
                if (id.ContainsKey((int)returnCd) && !processed.ContainsKey((int)returnCd))
                {
                    processed[(int)returnCd] = true;
                    break;
                }
                else
                {
                    returnCd = null;
                }

            }
            return returnCd;
        }

        private void btnRepeat_Click(object sender, EventArgs e)
        {
            SpeakWord(_currWord);
        }

        private void btnShowText_Click(object sender, EventArgs e)
        {
            displayWord.Text = _currWord;
        }

        private void checkWord_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (checkWord.Text.Trim().ToUpper().Equals(_currWord.Trim().ToUpper()))
                {
                    DisplayStats();
                    SpeakWord("Nextword");
                    MoveToNextWord();
                    checkWord.Text= string.Empty;
                }
                else
                {
                    _incorrectWords++;
                    SpeakWord("You are incorrect, please try again");
                }
                checkWord.Focus();
            }
            if ((int)e.KeyChar == (char)Keys.Space)
            {
                SpeakWord(_currWord);
                checkWord.Text = string.Empty;
                checkWord.Focus();
            }
            if ((int)e.KeyChar == (char)44)
            {
                _incorrectWords++;
                displayWord.Text = _currWord;
                checkWord.Text = string.Empty;
                checkWord.Focus();
            } 
        }

        void DisplayStats()
        {
            if (processed.Count != 0)
            {
                TotalWords.Text = id.Count.ToString();
                SpeltWords.Text = processed.Count.ToString();
                IncorrectWords.Text = _incorrectWords.ToString();
                PctCorrect.Text = (100 * ((processed.Count - _incorrectWords) / processed.Count)).ToString();
            }
        }

        private void ChooseFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Spelling Bee Files|*.bee;*.txt";
            openFileDialog1.InitialDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _currWordFile = openFileDialog1.FileName;
                ReInitWords();

            }

        }
    }
}
